/*
 * IC Project - a prototype to test a hypothesis of collective intelligence
 * Copyright (C) 2012 Paolo Conte - paolo@concavi.net
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ix.net.p2p.commands;

import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ix.data.entities.P2PMessage;
import org.ix.data.entities.PeerInfo;
import org.ix.net.BaseP2PServlet;

public class List extends BaseP2PServlet {

    protected void serve(HttpServletRequest request, HttpServletResponse response) {
        if (broker.size() == 0) {
            out.print(P2PMessage.RESPONSE_TYPE_ERROR + ": no peers found");
            return;
        }

        Iterator<PeerInfo> peers = broker.getPeers().iterator();
        out.print(P2PMessage.RESPONSE_TYPE_REPLY + ": ");
        while (peers.hasNext()) {
            PeerInfo peer = peers.next();
            out.println(peer.toP2PString());
        }
    }

    public String getServletInfo() {
        return "P2P " + P2PMessage.ACTION_LIST + " command";
    }
}
